﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IAC_2013.com.iac.form;
using IAC_2013.com.iac.bo;
using System.Data;
using System.Data.SqlClient;
using IAC_2013.com.iac.util;
using IAC_2013.com.iac.dbdao;

namespace IAC_2013.com.iac.action
{
    class UnitAction
    {
        SqlConnection conn = new SqlConnection(DBUtils.CONNECTION_STRING);
        SqlCommand cmd;
        SqlDataAdapter da;
        DataTable tb;
        public Boolean addUnit(UnitForm frm)
        {
            string sql = "insert into dv_don_vi values(";
            sql += "'" + frm.unitCode +"', ";
            sql += "N'"+ frm.unitName +"', ";
            sql += "N'"+ frm.description +"')";

            try {
                cmd = new SqlCommand();
                cmd.CommandText = sql;
                cmd.Connection = conn;
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                cmd.Dispose();
                return true;
            }
            catch (Exception) {
                return false;
            }
        }

        public Boolean editUnit(UnitForm frm)
        {
            string sql = "update dv_don_vi set ";
            sql += " ma_dv = '"+ frm.unitCode +"', ";
            sql += " ten_dv = N'"+ frm.unitName +"', ";
            sql += " mo_ta = N'"+ frm.description +"' ";
            sql += " where ";
            sql += " id_dv =" + frm.unitId;

            try
            {
                cmd = new SqlCommand(sql, conn);
                conn.Open();
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                conn.Close();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public Boolean deleteUnit(UnitForm frm)
        {
            string sql = "delete from dv_don_vi where id_dv = " + frm.unitId;

            try
            {
                cmd = new SqlCommand(sql, conn);
                conn.Open();
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                conn.Close();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public DataTable actionSearch(UnitForm frm)
        {
            string sql = "select * from dv_don_vi dv where 1 = 1 ";
            if (frm.unitCode != null && frm.unitCode != "")
            {
                sql += " and LOWER(dv.ma_dv) LIKE '" + "%" + frm.unitCode + "%" +"' ";
            }
            if (frm.unitName != null && frm.unitName != "")
            {
                sql += " and LOWER(dv.ten_dv) LIKE '"+ "%" + frm.unitName + "%" +"' ";
            }

            try
            {
                cmd = new SqlCommand(sql, conn);
                conn.Open();
                da = new SqlDataAdapter(cmd);
                tb = new DataTable();
                da.Fill(tb);
                cmd.Dispose();
                conn.Close();
                return tb;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
